package ui.activity.profit;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import net.ck.hzq.R;

import java.util.List;

import javax.inject.Inject;

import Bean.HistoricalincomeBean;
import base.BaseAct;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import event.NetErrorEvent;
import ui.activity.profit.biz.HistoricalIncomeBiz;
import ui.activity.profit.component.DaggerHistoricalIncomeComponent;
import ui.activity.profit.contract.HistoricalIncomeContract;
import ui.activity.profit.module.HistoricalIncomeModule;
import ui.activity.profit.presenter.HistoricalIncomePresenter;
import ui.adapter.HistoricalIcomeAdapter;


/**
 * 历史收益
 * Created by chenjian on 2017/8/4.
 */

public class HistoricalIncomeAct extends BaseAct implements HistoricalIncomeContract.View {


    @Inject
    HistoricalIncomePresenter presenter;
    @Inject
    HistoricalIncomeBiz biz;


    private TextView totalMoney;
    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;
    View headerView;

    private SharedPreferences sp;
    HistoricalIcomeAdapter adp;

    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_historical_income);
    }

    @Override
    protected void setup() {
        DaggerHistoricalIncomeComponent.builder().historicalIncomeModule(new HistoricalIncomeModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.historicalincome), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        sp = getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
        headerView = View.inflate(this, R.layout.header_historical_income, null);
        totalMoney = headerView.findViewById(R.id.historicalincome_txt);
        rv.setLayoutManager(new LinearLayoutManager(HistoricalIncomeAct.this));
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
            }
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getInfo(true, sp.getString("id", "0"));
            }
        });
        ptr.autoRefresh(true);
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }

    @Override
    public void upDateUI(List<HistoricalincomeBean.DataBean.ItemsBean> bean, String money) {
        if (adp == null) {
            adp = new HistoricalIcomeAdapter(HistoricalIncomeAct.this, bean);
            adp.addHeaderView(headerView);
            rv.setAdapter(adp);
        } else {
            adp.notifyDataSetChanged();
        }
        totalMoney.setText(money);
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }

    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }
}
